﻿using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;

[Serializable]
public class ActionItem
{
    public string Name;
    public Action Action;

    public ActionItem(string name, Action action)
    {
        Name = name;
        Action = action;
    }
}


public class BackPack:MonoBehaviour
{
    private Inventory inventory;

    [SerializeField] private Transform girdGroup;

    [SerializeField] private GameObject girdPrefab;
    [SerializeField] private ItemButton itemButton;

    [Header("按钮组")]
    [SerializeField] private Transform sortButtonGroup;
    [SerializeField] private SecondaryButton secondaryButton;

    private int girdNumber=18;

    bool isAccendOrder=true;
    private List<Item> currentItemList;
    private void Start()
    {
        inventory=new Inventory();
        currentItemList=new List<Item>();   

        SecondaryButtonSet("种类", new List<ActionItem>
        {
            new ActionItem("全部",() => ShowPanel(inventory.items)),
            new ActionItem("道具",() => ShowPanel(inventory.GetItemKind("道具"))),
            new ActionItem("武器",() => ShowPanel(inventory.GetItemKind("武器")))
        });

        SecondaryButtonSet("排序", new List<ActionItem>
        {
            new ActionItem
            (
                "数量",
                () =>
                {
                    var sortedByCountAsc =Utility.Sort(currentItemList, item => item.ItemInfo.count, isAccendOrder).ToList();
                    ShowPanel(sortedByCountAsc);
                    isAccendOrder=!isAccendOrder;
                }
            ),
            new ActionItem("稀有度",() => ShowPanel(inventory.GetItemKind("武器")))
        });

    }
    private void SecondaryButtonSet(string buttonName, List<ActionItem> Actions)
    {
        var clone = Instantiate(secondaryButton.gameObject, sortButtonGroup, false);
        clone.GetComponent<SecondaryButton>().Initialize(Actions);
        clone.GetComponent<SecondaryButton>().PrimaryButton(buttonName);
    }

    public void AddItem(Item item)
    {
        inventory.AddItem(item);
        UpdatePanel();
    }
    public void RemoveItem(Item item)
    {
        inventory.RemoveItem(item);
    }
    private void UpdatePanel()
    {
        ShowPanel(inventory.items);
    }


    private void ClearGird()
    {
        foreach (Transform item in girdGroup)
        {
            Destroy(item.gameObject);
        }
    }
    public void ShowPanel(List<Item> items)
    {
         ClearGird();
        currentItemList = items;
        foreach(var item in currentItemList)
        {
            var clone = Instantiate(itemButton.gameObject, girdGroup, false);
            clone.GetComponent<ItemButton>().Init(item);
        }
    }

  
    private void Update()
    {
        if(Input.GetKeyDown(KeyCode.Escape))
        {
            Debug.Log("测试增加物品");
            TestData(0, "张三", "道具",1);
            TestData(1, "李四", "道具",12);
            TestData(2, "王五", "道具", 1);
            TestData(3, "赵六", "武器",10);
        }
    }

    private void TestData(int id, string name, string kinds,int conut)
    {
        ItemData data = new ItemData();
        data.Id = id;
        data.Name=name;
        data.Kinds=kinds;
        Item item = new Item(data);
        item.ItemInfo.count=conut;
        AddItem(item);
    }
    // 通用排序方法

    #region     背包格子
    private void CreateGird(int number)
    {
        for (int i = 0; i < number; i++)
        {
            var newGrid = Instantiate(girdPrefab, girdGroup, false);
            newGrid.name="gird ("+i+")";
        }
    }
    //private void AddItemInBag(List<Item> items)
    //{
    //    ClearAllChildren();

    //    Debug.Log("增加物品");
    //    if (items == null)
    //    {
    //        Debug.Log("物体为空");
    //        return;
    //    }
    //    for (int i = 0; i < items.Count; i++)
    //    {
    //        var clone = Instantiate(itemButton.gameObject, girdGroup.GetChild(i).transform, false);
    //        clone.GetComponent<ItemButton>().Init(items[i]);
    //    }

    //}

    ////清除背包格子下面的子对象
    //public void ClearAllChildren()
    //{
    //    foreach (Transform childTransform in girdGroup.transform)
    //    {
    //        GameObject childObject = childTransform.gameObject;
    //        if (childObject.transform.childCount != 0)
    //        {
    //            ClearAllChildren(childObject);
    //        }
    //    }
    //}
    ////清除子对象
    //public void ClearAllChildren(GameObject parent)
    //{
    //    int childCount = parent.transform.childCount;

    //    for (int i = childCount - 1; i >= 0; i--)
    //    {
    //        Transform child = parent.transform.GetChild(i);
    //        ClearAllChildren(child.gameObject);
    //        Destroy(child.gameObject);
    //    }
    //}
    #endregion

}

